package _1_PIO.excel.创建;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 创建单元格
 */
public class _3_CreatingCells {

    @Test
    public void _2003(){

    }

    @Test
    public void _2007(){

        Workbook workbook = new XSSFWorkbook();
        CreationHelper creationHelper = workbook.getCreationHelper();

        //创建Sheet
        String singleSceneString = WorkbookUtil.createSafeSheetName("单场景");
        Sheet singleSceneSheet = workbook.createSheet(singleSceneString);

        //创建行（第1行）
        Row row = singleSceneSheet.createRow(0);
        //创建列-方法1（第1行，第1列）
        Cell cell = row.createCell(0);
        cell.setCellValue(1);
        //创建列-方法2（第1行，第2,3,4,5列）
        row.createCell(1).setCellValue(1.2);
        row.createCell(2).setCellValue(creationHelper.createRichTextString("This is a string"));
        row.createCell(3).setCellValue("a string too");
        row.createCell(4).setCellValue(true);

        //写入文件
        try(OutputStream outputStream = new FileOutputStream("D://workbook.xlsx")){
            workbook.write(outputStream);
        } catch (IOException e) {
            System.out.println("这里报异常");
            e.printStackTrace();
        }
    }
}
